#include "HeadTrackGraphics.h"
#include "Scene.h"
#include <fstream>

Scene* simulationScene = new Scene();

void initGL() {
	/* Background and foreground color */
	glClearColor(0.0,0.0,0.0,0.0);
	glColor3f(1.0,1.0,1.0);

	/* Select the projection matrix and reset it */
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	/* Our screen coordinates */
	gluOrtho2D(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT);

	/* work in fullscreen */
	glutFullScreen();
}


void setupGlut() 
{
	drawGrid = true;

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
	glutInitWindowPosition(0,0);
	glutCreateWindow("Head Tracking Demo");

	glutDisplayFunc(displayFunc);
	glutReshapeFunc(reshapeFunc);
	//	glutMouseFunc(mouseFunc);
	glutKeyboardFunc(keyboardFunc);

}

void displayFunc() {

	/* Clear the screen with the clearcolor */
	glClear(GL_COLOR_BUFFER_BIT);
	simulationScene->draw();

	if (drawGrid)
	{
		glBegin(GL_LINES);
			glColor3f(0.3, 0, 0.3);
			for (int i=1; i < NUM_REGIONS_HORIZONTAL; i++)
			{
				glVertex3f( i * (float)SCREEN_WIDTH / NUM_REGIONS_HORIZONTAL, 
							0, 
							0);
				glVertex3f( i * (float)SCREEN_WIDTH / NUM_REGIONS_HORIZONTAL, 
							SCREEN_HEIGHT, 
							0);
			}

			for (int i=1; i < NUM_REGIONS_VERTICAL; i++)
			{
				glVertex3f( 0,
							i * (float)SCREEN_HEIGHT / NUM_REGIONS_VERTICAL,
							0);
				glVertex3f( SCREEN_WIDTH,
							i * (float)SCREEN_HEIGHT / NUM_REGIONS_VERTICAL,
							0);
			}
		glEnd();

	}
	glutSwapBuffers ();
}


void reshapeFunc (int w, int h)
{
	//  Reset viewport
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, w, 0.0, h, -1.0, 1.0);
	glMatrixMode(GL_MODELVIEW);
}

void mouseFunc (int button, int state, int x, int y)
{
	switch (button)
	{
		//  Left Button Clicked
	case GLUT_LEFT_BUTTON:

		switch (state)
		{
			//  Pressed 
		case GLUT_DOWN:
			break;
			//  Released
		case GLUT_UP:
			break;
		}

		break;

		//  Middle Button clicked
	case GLUT_MIDDLE_BUTTON:

		switch (state)
		{
			//  Pressed
		case GLUT_DOWN:
			break;
			//  Released
		case GLUT_UP:
			break;
		}

		break;

		//  Right Button Clicked
	case GLUT_RIGHT_BUTTON:

		switch (state)
		{
			//  Pressed
		case GLUT_DOWN:
			break;
			//  Released
		case GLUT_UP:
			break;
		}

		break;

	}
}

void keyboardFunc (unsigned char key, int x, int y)
{
	switch (key)
	{
		// g toggles on/off the grid
		case 'g':
			drawGrid = !drawGrid;
			break;
		//  ESC key exits the application
		case 27:
			terminateGraphics();
			exit (1);
			break;
	}

	glutPostRedisplay ();
}

void terminateGraphics()
{
	ofstream fileOut("headTrack.txt");

	if (!fileOut)
	{
		cout << "Cannot open file!" << endl;
	}
	else 
	{
		FreeTrackData currentData;
		for (unsigned int i = 0; i < simulationScene->headTrackData.size(); i++)
		{
			currentData = simulationScene->headTrackData[i];

			fileOut << currentData.x				<< "\t" 
					<< currentData.y				<< "\t" 
					<< currentData.z				<< "\t\t"  
					<< currentData.yaw * 57.3		<< "\t" /* convert radians to degrees */ 
					<< currentData.pitch * 57.3		<< "\t" 
					<< currentData.roll * 57.3		<< "\t" << endl;
		}

		fileOut.close();
	}

	delete simulationScene;
}